    //
//  SettingViewController.m
//  RssReader
//
//  Created by Wang Shuguang on 12-5-25.
//  Copyright 2012 Dawn. All rights reserved.
//

#import "SettingViewController.h"
#import "ChannelEditViewController.h"


@implementation SettingViewController
@synthesize delegate;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.view.frame = CGRectMake(0, 20, 320, 460);
	self.view.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0];
	
	//
	channelList = [[UITableView alloc] initWithFrame:CGRectMake(0, 37, 320, 460-37) style:UITableViewStylePlain];
	channelList.showsVerticalScrollIndicator = YES;
	channelList.showsHorizontalScrollIndicator = NO;
	channelList.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
	channelList.separatorColor = [UIColor colorWithRed:224.0f/255.0f green:224.0f/255.0f blue:224.0f/255.0f alpha:1.0f];
	channelList.delegate = self;
	channelList.dataSource = self;
	channelList.rowHeight = 40;
	channelList.backgroundColor = [UIColor clearColor];
	channelList.sectionFooterHeight = 40;
	
	//更多按钮
    UIView *itemsFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
    channelList.tableFooterView = itemsFooterView;
    [itemsFooterView release];
    UIButton *moreButton = [PublicMethods buttonWithImage:nil backgroundImage:[UIImage imageNamed:@"more_item.png"] highlightedBackgroundImage:[UIImage imageNamed:@"more_item_heighlight.png"]];
    moreButton.frame = CGRectMake(0, 0, 304, 33);
    moreButton.center = CGPointMake(320/2, 33.0/2 + 4);
    [moreButton setTitle:@"添加频道" forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0/255.0f alpha:1] forState:UIControlStateNormal];
    [moreButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [moreButton addTarget:self action:@selector(addChannel:) forControlEvents:UIControlEventTouchUpInside];
    [itemsFooterView addSubview:moreButton];

	
	[self.view addSubview:channelList];
	[channelList release];
	
	
	
	
	
	//导航条
	UIImageView *navView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 37)];
	navView.image = [UIImage noCacheImageNamed:@"navbar_background.png"];
	navView.userInteractionEnabled = YES;
	[self.view addSubview:navView];
	//返回按钮
	UIButton *backButton = [PublicMethods buttonWithImage:nil 
										  backgroundImage:[UIImage noCacheImageNamed:@"navbar_back_normal.png"] 
							   highlightedBackgroundImage:[UIImage noCacheImageNamed:@"navbar_back_heighlight.png"]];
	backButton.frame = CGRectMake(0, 0, 47, 35);
	[backButton addTarget:self action:@selector(navBack:) forControlEvents:UIControlEventTouchUpInside];
	[navView addSubview:backButton];
	//编辑按钮
	editButton = [PublicMethods buttonWithImage:nil 
										  backgroundImage:[UIImage noCacheImageNamed:@"white_btn_press.png"] 
							   highlightedBackgroundImage:[UIImage noCacheImageNamed:@"white_btn_normal.png"]];
	editButton.frame = CGRectMake(270, 3, 46, 30);
	editButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
	[editButton setTitle:@"编辑" forState:UIControlStateNormal];
	[editButton setTitleColor:[UIColor colorWithRed:30.0f/255.0f green:140.0f/255.0f blue:199.0f/255.0f alpha:1] forState:UIControlStateHighlighted];
	[editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[editButton addTarget:self action:@selector(beginEditing:) forControlEvents:UIControlEventTouchUpInside];
	[navView addSubview:editButton];
	
	rss = [Rss shareRss];
}
-(void)beginEditing:(id)sender{
	if ([editButton.titleLabel.text isEqualToString:@"编辑"]) {
		[channelList setEditing:YES animated:YES];
		[editButton setTitle:@"完成" forState:UIControlStateNormal];
	}else{
		[channelList setEditing:NO animated:YES];
		[editButton setTitle:@"编辑" forState:UIControlStateNormal];
	}
	
}
-(void) navBack:(id)sender{
	[channelList setEditing:NO];
	[self.navigationController popViewControllerAnimated:YES];
}

-(void) addChannel:(id)sender{
	UIAlertView *alertView = [[UIAlertView alloc] init];
	alertView.frame = CGRectMake(20, 200, 280, 60);
	alertView.title = @"添加频道";
	alertView.message = @"\n\n";
	alertView.delegate = self;
	[alertView addButtonWithTitle:@"取消"];
	[alertView addButtonWithTitle:@"确定"];
	UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 240, 30)];
	textField.borderStyle = UITextBorderStyleRoundedRect;
	textField.tag = 2012;
	[alertView addSubview:textField];
	[textField release];
	[alertView show];
}
#pragma mark -
#pragma mark UIAlertViewDelegate
- (void) alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{  
	if (buttonIndex == 0) {
		[alertView release];
	}else if (buttonIndex == 1) {
		//得到输入框  
		UITextField *tf = (UITextField *)[alertView viewWithTag:2012];
		NSString *url = tf.text;
		tf.hidden = YES;
		[alertView release];
		rss.delegate = self;
		[rss addChannel:url];
		
		[self.view startLoading2];
	}
}  

#pragma mark -
#pragma mark RssDelegate
-(void) channelAdded:(NSDictionary *)dict{
	if ([delegate respondsToSelector:@selector(channelsCommitEditing)]) {
		[delegate channelsCommitEditing];
		[channelList reloadData];
		[rss saveConfig];
	}
	[self.view endLoading];
	
	[PublicMethods showAlertTitle:@"" Message:@"频道添加成功"];
}

-(void) channelAddedFailed:(NSString *)errorMsg{
	[self.view endLoading];
	
	[PublicMethods showAlertTitle:@"" Message:errorMsg];
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
	ChannelEditViewController *channelEditVC = [[ChannelEditViewController alloc] init];
	channelEditVC.channelIndex = indexPath.row;
	[self.navigationController pushViewController:channelEditVC animated:YES];
	[channelEditVC release];
}

#pragma mark -
#pragma mark UITableViewDataSource
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"channel"];
	if (!cell) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"channel"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
	}
	NSDictionary *dict = [rss.channels objectAtIndex:indexPath.row];
	cell.textLabel.text = [dict objectForKey:@"Title"];
	return cell;
}

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	if (rss.channels) {
		return [rss.channels count];
	}else {
		return 0;
	}
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{ 
	return @"删除"; 
} 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
	//允许移动
	return YES;
}

- (BOOL) tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
	return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
	
	NSLog(@"%d----%d",sourceIndexPath.row,destinationIndexPath.row);
	//交换数据
	NSMutableDictionary *tdict = [[rss.channels objectAtIndex:sourceIndexPath.row] retain];
	[rss.channels removeObjectAtIndex:sourceIndexPath.row];	
	[rss.channels insertObject:tdict atIndex:destinationIndexPath.row];
	[tdict release];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
	if (editingStyle == UITableViewCellEditingStyleDelete) {
		[rss.channels removeObjectAtIndex:indexPath.row];
		[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationFade];
	}
}

- (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath{
	if ([delegate respondsToSelector:@selector(channelsCommitEditing)]) {
		[delegate channelsCommitEditing];
		[rss saveConfig];
		
		
	}
}



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
